package com.ruoyi.core.controller;

import com.ruoyi.common.constant.ErrorCode;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.core.model.dto.questionsumbit.QuestionSubmitAddRequest;
import com.ruoyi.core.service.QuestionSubmitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 题目管理
 *
 * @author Shier
 */
@RestController
@RequestMapping("/question")
@Slf4j
public class QuestionController {

    @Resource
    private QuestionSubmitService questionSubmitService;


    /**
     * 提交题目
     *
     * @param questionSubmitAddRequest
     * @return 提交记录 id
     */
    @PostMapping("/subimt")
    public AjaxResult doQuestionSubmit(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest) {
        if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {
            return AjaxResult.error(ErrorCode.PARAMS_ERROR.getMessage());
        }
        return questionSubmitService.doQuestionSubmit(questionSubmitAddRequest);
    }

//
//
//    /**
//     * 提交题目
//     *
//     * @param questionAddRequest
//     * @param request
//     * @return
//     */
//    @PostMapping("/add")
////    @ApiOperation(value = "添加题目")
//    public AjaxResult addQuestion(@RequestBody QuestionAddRequest questionAddRequest, HttpServletRequest request) {
//        if (questionAddRequest == null) {
//            return AjaxResult.error(ErrorCode.PARAMS_ERROR.getMessage());
//        }
//        Question question = new Question();
//        BeanUtils.copyProperties(questionAddRequest, question);
//        List<String> tags = questionAddRequest.getTags();
//        if (tags != null) {
//            question.setTags(JSONUtils.toJSONString(tags));
//        }
//        List<JudgeCase> judgeCase = questionAddRequest.getJudgeCase();
//        if (judgeCase != null) {
//            question.setJudgeCase(JSONUtils.toJSONString(judgeCase));
//        }
//        JudgeConfig judgeConfig = questionAddRequest.getJudgeConfig();
//        if (judgeConfig != null) {
//            question.setJudgeConfig(JSONUtils.toJSONString(judgeConfig));
//        }
//        questionService.validQuestion(question, true);
//
//        User loginUser = userService.getLoginUser(request);
//        question.setUserId(loginUser.getId());
//        question.setFavourNum(0);
//        question.setThumbNum(0);
//
//        boolean result = questionService.save(question);
//        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
//        long newQuestionId = question.getId();
//        return ResultUtils.success(newQuestionId);
//    }
//
//    /**
//     * 删除题目
//     *
//     * @param deleteRequest
//     * @param request
//     * @return
//     */
//    @PostMapping("/delete")
//    @ApiOperation(value = "删除题目")
//    public BaseResponse<Boolean> deleteQuestion(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
//        if (deleteRequest == null || deleteRequest.getId() <= 0) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        User user = userService.getLoginUser(request);
//        long id = deleteRequest.getId();
//        // 判断是否存在
//        Question oldQuestion = questionService.getById(id);
//        ThrowUtils.throwIf(oldQuestion == null, ErrorCode.NOT_FOUND_ERROR);
//        // 仅本人或管理员可删除
//        if (!oldQuestion.getUserId().equals(user.getId()) && !userService.isAdmin(request)) {
//            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
//        }
//        boolean b = questionService.removeById(id);
//        return ResultUtils.success(b);
//    }
//
//    /**
//     * 更新（仅管理员）
//     *
//     * @param questionUpdateRequest
//     * @return
//     */
//    @PostMapping("/update")
//    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
//    @ApiOperation(value = "管理员更新信息")
//    public BaseResponse<Boolean> updateQuestion(@RequestBody QuestionUpdateRequest questionUpdateRequest) {
//        if (questionUpdateRequest == null || questionUpdateRequest.getId() <= 0) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        Question question = new Question();
//        BeanUtils.copyProperties(questionUpdateRequest, question);
//        List<String> tags = questionUpdateRequest.getTags();
//        if (tags != null) {
//            question.setTags(GSON.toJson(tags));
//        }
//        List<JudgeCase> judgeCase = questionUpdateRequest.getJudgeCase();
//        if (judgeCase != null) {
//            question.setJudgeCase(GSON.toJson(judgeCase));
//        }
//        JudgeConfig judgeConfig = questionUpdateRequest.getJudgeConfig();
//        if (judgeConfig != null) {
//            question.setJudgeConfig(GSON.toJson(judgeConfig));
//        }
//        // 参数校验
//        questionService.validQuestion(question, false);
//        long id = questionUpdateRequest.getId();
//        // 判断是否存在
//        Question oldQuestion = questionService.getById(id);
//        ThrowUtils.throwIf(oldQuestion == null, ErrorCode.NOT_FOUND_ERROR);
//        boolean result = questionService.updateById(question);
//        return ResultUtils.success(result);
//    }
//
//    /**
//     * 根据 id 获取 (脱敏)
//     *
//     * @param id
//     * @return
//     */
//    @GetMapping("/get/vo")
//    @ApiOperation(value = "获取题目信息")
//    public BaseResponse<QuestionVO> getQuestionVoById(long id, HttpServletRequest request) {
//        if (id <= 0) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        Question question = questionService.getById(id);
//        if (question == null) {
//            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
//        }
//        return ResultUtils.success(questionService.getQuestionVO(question, request));
//    }
//
//    /**
//     * 根据 id 获取
//     *
//     * @param id
//     * @return
//     */
//    @GetMapping("/get")
//    @ApiOperation(value = "获取题目信息")
//    public BaseResponse<Question> getQuestionById(long id, HttpServletRequest request) {
//        if (id <= 0) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        Question question = questionService.getById(id);
//        if (question == null) {
//            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
//        }
//        // 用户权限判断
//        User loginUser = userService.getLoginUser(request);
//        // 非本人或者管理员，不能获取到题目所有信息
//        if (!question.getUserId().equals(loginUser.getId()) && userService.isAdmin(loginUser)) {
//            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权限");
//        }
//        return ResultUtils.success(question);
//    }
//
//    /**
//     * 分页获取列表（封装类）
//     *
//     * @param questionQueryRequest
//     * @param request
//     * @return
//     */
//    @PostMapping("/list/page/vo")
//    @ApiOperation(value = "分页获取题目列表")
//    public BaseResponse<Page<QuestionVO>> listQuestionVOByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
//                                                               HttpServletRequest request) {
//        long current = questionQueryRequest.getCurrent();
//        long size = questionQueryRequest.getPageSize();
//        // 限制爬虫
//        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
//        Page<Question> questionPage = questionService.page(new Page<>(current, size),
//                questionService.getQueryWrapper(questionQueryRequest));
//        return ResultUtils.success(questionService.getQuestionVOPage(questionPage, request));
//    }
//
//    /**
//     * 分页获取题目列表（仅管理员）
//     *
//     * @param questionQueryRequest
//     * @param request
//     * @return
//     */
//    @PostMapping("/list/page")
//    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
//    @ApiOperation(value = "管理员分页获取题目列表（所有信息）")
//    public BaseResponse<Page<Question>> listQuestionByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
//                                                           HttpServletRequest request) {
//        long current = questionQueryRequest.getCurrent();
//        long size = questionQueryRequest.getPageSize();
//        Page<Question> questionPage = questionService.page(new Page<>(current, size),
//                questionService.getQueryWrapper(questionQueryRequest));
//        return ResultUtils.success(questionPage);
//    }
//
//    /**
//     * 分页获取当前用户创建的资源列表
//     *
//     * @param questionQueryRequest
//     * @param request
//     * @return
//     */
//    @PostMapping("/my/list/page/vo")
//    @ApiOperation(value = "分页获取当前用户创建的题目列表")
//    public BaseResponse<Page<QuestionVO>> listMyQuestionVOByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
//                                                                 HttpServletRequest request) {
//        if (questionQueryRequest == null) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        User loginUser = userService.getLoginUser(request);
//        questionQueryRequest.setUserId(loginUser.getId());
//        long current = questionQueryRequest.getCurrent();
//        long size = questionQueryRequest.getPageSize();
//        // 限制爬虫
//        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
//        Page<Question> questionPage = questionService.page(new Page<>(current, size),
//                questionService.getQueryWrapper(questionQueryRequest));
//        return ResultUtils.success(questionService.getQuestionVOPage(questionPage, request));
//    }
//
//    /**
//     * 编辑（用户）
//     *
//     * @param questionEditRequest
//     * @param request
//     * @return
//     */
//    @PostMapping("/edit")
//    @ApiOperation(value = "编辑题目")
//    public BaseResponse<Boolean> editQuestion(@RequestBody QuestionEditRequest questionEditRequest, HttpServletRequest request) {
//        if (questionEditRequest == null || questionEditRequest.getId() <= 0) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        Question question = new Question();
//        BeanUtils.copyProperties(questionEditRequest, question);
//        List<String> tags = questionEditRequest.getTags();
//        if (tags != null) {
//            question.setTags(GSON.toJson(tags));
//        }
//        List<JudgeCase> judgeCase = questionEditRequest.getJudgeCase();
//        if (judgeCase != null) {
//            question.setJudgeCase(GSON.toJson(judgeCase));
//        }
//        JudgeConfig judgeConfig = questionEditRequest.getJudgeConfig();
//        if (judgeConfig != null) {
//            question.setJudgeConfig(GSON.toJson(judgeConfig));
//        }
//        // 参数校验
//        questionService.validQuestion(question, false);
//        User loginUser = userService.getLoginUser(request);
//        long id = questionEditRequest.getId();
//        // 判断是否存在
//        Question oldQuestion = questionService.getById(id);
//        ThrowUtils.throwIf(oldQuestion == null, ErrorCode.NOT_FOUND_ERROR);
//        // 仅本人或管理员可编辑
//        if (!oldQuestion.getUserId().equals(loginUser.getId()) && !userService.isAdmin(loginUser)) {
//            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
//        }
//        boolean result = questionService.updateById(question);
//        return ResultUtils.success(result);
//    }


//
//    /**
//     * 分页获取题目提交列表（除了管理员外，其他普通用户只能看到非答案、提交的代码等公开信息）
//     *
//     * @param questionSubmitQueryRequest
//     * @param request
//     * @return
//     */
//    @PostMapping("/question_submit/list/page")
//    @ApiOperation(value = "分页获取题目提交列表")
//    public BaseResponse<Page<QuestionSubmitVO>> listQuestionSubmitByPage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest,
//                                                                         HttpServletRequest request) {
//        long current = questionSubmitQueryRequest.getCurrent();
//        long pageSize = questionSubmitQueryRequest.getPageSize();
//
//        // 从数据库中查询到原始的题目提交信息
//        Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(current, pageSize),
//                questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
//        final User loginUer = userService.getLoginUser(request);
//        // 返回脱敏信息
//        return ResultUtils.success(questionSubmitService.getQuestionSubmitVOPage(questionSubmitPage, loginUer));
//    }
}
